package com.xhtt.modules.app.resolver;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhtt.common.utils.Constant;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.app.interceptor.AuthorizationInterceptor;
import com.xhtt.modules.sys.entity.SysDeptEntity;
import com.xhtt.modules.sys.entity.SysRoleEntity;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysRoleService;
import com.xhtt.modules.sys.service.SysUserDeptService;
import com.xhtt.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 有@LoginUser注解的方法参数，注入当前登录用户
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 22:02
 */
@Component
public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysUserDeptService sysUserDeptService;
    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().isAssignableFrom(SysUserEntity.class) && parameter.hasParameterAnnotation(LoginUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
                                  NativeWebRequest request, WebDataBinderFactory factory) throws Exception {
        //获取用户ID
        Object object = request.getAttribute(AuthorizationInterceptor.USER_KEY, RequestAttributes.SCOPE_REQUEST);
        if (object == null) {
            return null;
        }

        //获取用户信息
        SysUserEntity user = userService.getById((Long) object);
//        if (Constant.SUPER_ADMIN != user.getUserId()) {
            SysDeptEntity dept = sysUserDeptService.getDeptName(user.getUserId());
            if (null != dept) {
                user.setDeptId(dept.getDeptId().toString());
                user.setDeptName(dept.getName());
            }

            String roleName = sysRoleService.getRoleName(user.getUserId());

            user.setRoleName(roleName);
//        }
        return user;
    }

    private SysRoleEntity getParent(int roleId) {
        SysRoleEntity role = sysRoleService.getOne(new QueryWrapper<SysRoleEntity>().eq(SysRoleEntity.ROLEID, roleId).select(SysRoleEntity.ROLEID, SysRoleEntity.NAME, SysRoleEntity.PARENTID, SysRoleEntity.TYPE));
        if (-1 != role.getParentId().intValue()) {
            role = getParent(role.getParentId().intValue());
        }
        return role;
    }
}
